package com.atguigu.yygh.hosp.service.impl;

import com.atguigu.yygh.hosp.repository.DepartmentRepository;
import com.atguigu.yygh.hosp.service.DepartmentService;
import com.atguigu.yygh.hosp.util.ApiUtils;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.vo.hosp.DepartmentQueryVo;

import com.atguigu.yygh.vo.hosp.DepartmentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentRepository departmentRepository;

    //根据传的map保存科室
    @Override
    public void save(Map<String, Object> map) {
        Department department = ApiUtils.mapToClass(map, Department.class);
        //按照spring的命名规范创建方法
        Department departmentExist = departmentRepository.
                getDepartmentByHoscodeAndDepcode(department.getHoscode(), department.getDepcode());
        if (departmentExist != null) {
            updateDepartment(department, departmentExist);
            return;
        }
        saveDepartment(department);
    }

    //获取科室列表（分页）
    @Override
    public Page<Department> findPageDepartment(int page, int limit, DepartmentQueryVo departmentQueryVo) {
        //创建Department对象以及转换
        Department department = getDepartmentByDepartmentVo(departmentQueryVo);
        //创建Pageable对象
        Pageable pageable = PageRequest.of(page - 1, limit);
        //创建ExampleMatcher对象
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING) //模糊查询
                .withIgnoreCase(true); //忽略大小写

        Example<Department> example = Example.of(department, matcher);
        Page<Department> all = departmentRepository.findAll(example, pageable);
        return all;
    }

    //根据hoscode和depcode删除科室
    @Override
    public void remove(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if (department == null) {
            return;
        }
        departmentRepository.deleteById(department.getId());
    }

    //根据医院编号 查询医院所有科室列表
    @Override
    public List<DepartmentVo> findDeptTree(String hoscode) {
        List<DepartmentVo> result = new ArrayList<>();
        //获取所有大科室信息
        List<Department> departmentList = getDeptListByHoscode(hoscode);
        //根据大科室编号bigCode分组
        Map<String, List<Department>> departmentMap =
                departmentList.stream().collect(Collectors.groupingBy(Department::getBigcode));
        //遍历map集合
        for (Map.Entry<String, List<Department>> entry : departmentMap.entrySet()) {
            //大科室编号对应的全局数据(里面存着小科室)
            List<Department> deptParentList = entry.getValue();
            //封装大科室
            DepartmentVo parentDept = getParentDept(entry);
            //封装小科室
            List<DepartmentVo> deptChildList = getChildDeptList(deptParentList);
            //封装小到大里
            parentDept.setChildren(deptChildList);
            result.add(parentDept);
        }
        return result;
    }

    @Override
    public String getDepName(String hoscode, String depcode) {
        return getDeptByHoscodeAndDepcode(hoscode, depcode).getDepname();
    }

    @Override
    public Department getDeptByHoscodeAndDepcode(String hoscode, String depcode) {
        return departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
    }


    //封装大科室
    private DepartmentVo getParentDept(Map.Entry<String, List<Department>> entry) {
        String bigcode = entry.getKey();
        //大科室编号对应的数据
        List<Department> deptParentList = entry.getValue();
        DepartmentVo departmentVo = new DepartmentVo();
        departmentVo.setDepcode(bigcode);
        departmentVo.setDepname(deptParentList.get(0).getBigname());
        return departmentVo;
    }

    //封装小科室
    private List<DepartmentVo> getChildDeptList(List<Department> deptParentList) {
        List<DepartmentVo> deptChildList = new ArrayList<>();
        for (Department department : deptParentList) {
            DepartmentVo departmentVo = new DepartmentVo();
            departmentVo.setDepcode(department.getDepcode());
            departmentVo.setDepname(department.getDepname());
            deptChildList.add(departmentVo);
        }
        return deptChildList;
    }

    //根据hoscode获取所有科室
    private List<Department> getDeptListByHoscode(String hoscode) {
        Department departmentQuery = new Department();
        departmentQuery.setHoscode(hoscode);
        Example<Department> departmentExample = Example.of(departmentQuery);
        List<Department> all = departmentRepository.findAll(departmentExample);
        return all;
    }

    //根据departmentQueryVo获取Department
    private Department getDepartmentByDepartmentVo(DepartmentQueryVo departmentQueryVo) {
        Department department = new Department();
        department.setIsDeleted(0);
        BeanUtils.copyProperties(departmentQueryVo, department);
        return department;
    }

    //更新
    private void updateDepartment(Department department, Department departmentExist) {
        department.setUpdateTime(new Date());
        department.setIsDeleted(0);
        department.setCreateTime(departmentExist.getCreateTime());
        departmentRepository.save(department);
    }

    //保存
    private void saveDepartment(Department department) {
        department.setCreateTime(new Date());
        department.setCreateTime(new Date());
        department.setIsDeleted(0);
        departmentRepository.save(department);
    }

}
